呼應昨天介紹的資料交換格式,今天就要動手實作小程式啦!
背單字一直是我學生時期的惡夢(其實現在也是 ><),總覺得一背就忘,效果不彰。那如果能透過程式打造一個專屬的單字測驗小工具,是不是能讓背英文更有效率,也更有趣呢?
這次小程式的設計目標很簡單:
由電腦隨機出題 → 我們輸入答案 → 透過反覆測驗來加深記憶。
那麼就讓我們一起開始動手,寫出這個單字背誦測驗小程式吧!
首先我們需要載入用JSON檔案做儲存的英文加中文單字題庫,如果 JSON 檔案不存在或空檔,則使用預設題庫 DEFAULT_WORDS,並且支援未來新增或修改單字後,將題庫寫回 JSON。
[
{"word": "apple", "meaning": "蘋果"},
{"word": "book", "meaning": "書"},
{"word": "cat", "meaning": "貓"}
]
def load_words():
if os.path.exists(WORDS_FILE):
try:
with open(WORDS_FILE, "r", encoding="utf-8") as f:
data = f.read().strip()
if not data:
return DEFAULT_WORDS
return json.loads(data)
except json.JSONDecodeError:
return DEFAULT_WORDS
else:
return DEFAULT_WORDS
def save_words(words):
with open(WORDS_FILE, "w", encoding="utf-8") as f:
json.dump(words, f, ensure_ascii=False, indent=2)
記錄每次測驗的分數、題目數、答對數、正確率、測驗模式、時間,並存成 scores.json。
def load_scores():
if os.path.exists(SCORES_FILE):
with open(SCORES_FILE, "r", encoding="utf-8") as f:
return json.load(f)
else:
return []
def save_score(mode, total, correct):
scores = load_scores()
score_entry = {
"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
"mode": mode,
"total": total,
"correct": correct,
"rate": f"{correct/total*100:.1f}%"
}
scores.append(score_entry)
with open(SCORES_FILE, "w", encoding="utf-8") as f:
json.dump(scores, f, ensure_ascii=False, indent=2)
self.entry = tk.Entry(self.quiz_frame, font=("Arial", 14))
self.entry.pack(pady=5)
self.entry.bind("<Return>", lambda event: self.check_answer())
self.submit_btn = tk.Button(self.quiz_frame, text="提交", command=self.check_answer)
self.submit_btn.pack(pady=10)
測驗邏輯清楚分層:出題 → 答題 → 檢查 → 回饋 → 下一題
def show_question(self):
if self.index < len(self.words):
item = self.words[self.index]
if self.mode == "en2zh":
self.question_label.config(text=item["word"])
else:
self.question_label.config(text=item["meaning"])
self.entry.delete(0, tk.END)
self.feedback.config(text="")
else:
self.end_quiz()
def check_answer(self):
ans = self.entry.get().strip()
item = self.words[self.index]
correct = item["meaning"] if self.mode == "en2zh" else item["word"]
...
利用GUI 顯示完整歷史分數,並將 JSON 內容整理成易讀文字格式。
def show_scores(self):
scores = load_scores()
if not scores:
messagebox.showinfo("歷史紀錄", "目前沒有紀錄。")
return
top = tk.Toplevel(self.master)
text_widget = tk.Text(top, width=60, height=20)
text_widget.pack()
display_text = ""
for s in scores:
display_text += f"{s['time']} | {s['mode']} | 題目數: {s['total']} | 答對: {s['correct']} | 正確率: {s['rate']}\n"
text_widget.insert("1.0", display_text)
text_widget.config(state="disabled")
這次製作的小程式,讓我把昨天學到的 JSON 知識實際運用了一部分。雖然資料交換的功能還沒有完全發揮,但透過實作題庫操作,我還是感到滿滿的成就感。
更棒的是,我也能藉由這個單字背誦程式來增進自己的英文能力(總算不用再依賴付費 APP 了!)。未來我希望能加入更多功能與題庫,讓這個小程式真正成為一個實用又個人化的學習工具。